package dyyx.kumo;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.kennycason.kumo.palette.LinearGradientColorPalette;

public class KumoDemo {

	public static void main(String[] args) throws Exception {

		//  词频，词语最短长度 
		FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
		frequencyAnalyzer.setWordFrequenciesToReturn(600);
		frequencyAnalyzer.setMinWordLength(2);

		// 中文分析器
		frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer());
		
		String docFile = "/Users/dugang/fun/hellocode/web/docs/201909/hive-data-model.txt";
		String imgFile = "/Users/dugang/tmp/kumo.jpg";
		
		long start = System.currentTimeMillis();
		// 指定文本文件路径，生成词频集合
		final List<WordFrequency> wordFrequencyList = frequencyAnalyzer.load(docFile);
		long end = System.currentTimeMillis();
		long time = end - start;

		
		int num = wordFrequencyList.size();
		System.out.println("wordFrequencyList size "+num);
		for(WordFrequency wf:wordFrequencyList){
			System.out.println(wf.getWord()+"="+wf.getFrequency());
		}
		
		
		// 图片分辨率
		Dimension dimension = new Dimension(1920, 1080);
		
		// 
		WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
		// 边界 
		// wordCloud.setPadding(2);
		// 字体
		// java.awt.Font font = new java.awt.Font("STSong-Light", 2, 20);
		// 设置词云显示颜色 越靠前 词频越高 
		wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));
		// wordCloud.setKumoFont(new KumoFont(font));
		//  背景色
		// wordCloud.setBackgroundColor(new Color(255, 255, 255));		
		// 背景图层为圆形
		// wordCloud.setBackground(new CircleBackground(255));
		// wordCloud.setFontScalar(new SqrtFontScalar(12, 45));
		// 生成词云
		wordCloud.build(wordFrequencyList);
		wordCloud.writeToFile(imgFile);
		
		System.out.println("img build done,"+imgFile);
		System.out.println("time="+time);


	}
}
